// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from '../../store'
import '../../styles/common.css'

//FastClick will cause some problem like you have to double click the button to trigger event, need to look at this future
//const FastClick = require('fastclick')
//FastClick.attach(document.body)

import cropImageMobile from 'vue-crop-image-mobile'
Vue.use(cropImageMobile)

import {AjaxPlugin, LoadingPlugin, ToastPlugin, AlertPlugin} from 'vux'
Vue.use(LoadingPlugin)

// 添加一个请求拦截器
Vue.axios.interceptors.request.use(function (config) {
    if (store.state.user.token) {
        config.headers.Authorization = `${store.state.user.token}`;
    }
    return config;
  }, function (error) {
    // Do something with request error
    return Promise.reject(error);
  });

// 添加一个响应拦截器
Vue.axios.interceptors.response.use(function (response) {
    if(response.data.code=='1001'||response.data.code=='1002'){//具体的判断token失效的参数
        store.dispatch('deleteToken')
        store.dispatch('deleteUser')
        //sessionStorage.setItem("LoginUser",'{}')
        alert(response.data.msg);
        location.assign('/login/login.html')
    }else{
        return response
    }
  }, function (error) {
        if (error.response) {
            switch (error.response.status) {
                case 401:
                   store.dispatch('deleteToken')
                   store.dispatch('deleteUser')
                   location.assign('/login/login.html')
            }
        }
    return Promise.reject(error);
  }
);

//跨页面时，需要显示取出需要的数据
store.commit('FETCH_TOKEN') //JSON.parse(window.sessionStorage.getItem('user'))
store.commit('FETCH_USER_MODEL')
if (store.state.user.token){
  //这里是防止用户手动刷新页面，整个app要重新加载,动态新增的路由，会消失，所以我们重新add一次
  //let routes = []
  //MenuUtils(routes,data)
  //router.addRoutes(routes)
  //window.sessionStorage.removeItem('isLoadNodes')
}

store.commit('FETCH_LAST_LOGIN_AS')
if (store.state.user.lastLoginAs == 'teacher'){
	location.replace('/mavinTeacher')
}else{
	new Vue({
		el: '#app',
		router,
		store,
		template: '<App/>',
		components: { App }
	})
}

